

//
//  LoadingView.m
//  界面动画集
//
//  Created by YYY on 2017/7/4.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

#define kTopPointColor    [UIColor colorWithRed:90 / 255.0 green:200 / 255.0 blue:200 / 255.0 alpha:1.0].CGColor
#define kLeftPointColor   [UIColor colorWithRed:250 / 255.0 green:85 / 255.0 blue:78 / 255.0 alpha:1.0].CGColor
#define kBottomPointColor [UIColor colorWithRed:92 / 255.0 green:201 / 255.0 blue:105 / 255.0 alpha:1.0].CGColor
#define kRightPointColor  [UIColor colorWithRed:253 / 255.0 green:175 / 255.0 blue:75 / 255.0 alpha:1.0].CGColor

#define kTopPoint  CGPointMake(50, 15)
#define kLeftPoint  CGPointMake(15, 50)
#define kBottomPoint  CGPointMake(50, 85)
#define kRightPoint  CGPointMake(85, 50)

#define kRadius 13

#import "LoadingView.h"

@interface LoadingView ()

@property(nonatomic,strong)CAShapeLayer *topPointLayer;

@property(nonatomic,strong)CAShapeLayer *leftPointLayer;

@property(nonatomic,strong)CAShapeLayer *bottomPointLayer;

@property(nonatomic,strong)CAShapeLayer *rightPointLayer;

@end

@implementation LoadingView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        //添加顶部、左边、底部、右边四个点
        [self.layer addSublayer:self.topPointLayer];
        [self.layer addSublayer:self.leftPointLayer];
        [self.layer addSublayer:self.bottomPointLayer];
        [self.layer addSublayer:self.rightPointLayer];

    }
    return self;
}

//顶部
-(CAShapeLayer *)topPointLayer
{
    if (!_topPointLayer)
    {
        _topPointLayer = [self layerWithPoint:kTopPoint color:kTopPointColor];
    }
    return _topPointLayer;
}
//左边
-(CAShapeLayer *)leftPointLayer
{
    if (!_leftPointLayer)
    {
        _leftPointLayer = [self layerWithPoint:kLeftPoint color:kLeftPointColor];
    }
    return _leftPointLayer;
}
//底部
-(CAShapeLayer *)bottomPointLayer
{
    if (!_bottomPointLayer)
    {
        _bottomPointLayer = [self layerWithPoint:kBottomPoint color:kBottomPointColor];
    }
    return _bottomPointLayer;
}
-(CAShapeLayer *)rightPointLayer
{
    if (!_rightPointLayer)
    {
        _rightPointLayer = [self layerWithPoint:kRightPoint color:kRightPointColor];
    }
    return _rightPointLayer;
}

-(CAShapeLayer *)layerWithPoint:(CGPoint)point color:(CGColorRef)color
{
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = CGRectMake(point.x-kRadius, point.y-kRadius, kRadius*2, kRadius*2);
    layer.fillColor = color;
    layer.strokeColor = color;
    layer.path = [self drawPointPath];
    return layer;
}

//绘制路径
-(CGPathRef)drawPointPath
{
    return [UIBezierPath bezierPathWithArcCenter:CGPointMake(kRadius, kRadius) radius:kRadius startAngle:0 endAngle:2*M_PI clockwise:YES].CGPath;
}

//开始动画
-(void)startAnimation
{
    [self addAnimationForPointWith:_topPointLayer x:0 y:10];
    [self addAnimationForPointWith:_leftPointLayer x:10 y:0];
    [self addAnimationForPointWith:_bottomPointLayer x:0 y:-10];
    [self addAnimationForPointWith:_rightPointLayer x:-10 y:0];
    //添加旋转动画
    [self addRotationAnimation:self.layer];
}

//停止动画
-(void)stopAnimation
{
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.duration = 2.0f;
    opacityAnimation.toValue = @(0);
    //opacityAnimation.removedOnCompletion = NO;
    opacityAnimation.fillMode = kCAFillModeForwards;
    opacityAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    
    [_topPointLayer addAnimation:opacityAnimation forKey:nil];
    [_leftPointLayer addAnimation:opacityAnimation forKey:nil];
    [_bottomPointLayer addAnimation:opacityAnimation forKey:nil];
    [_rightPointLayer addAnimation:opacityAnimation forKey:nil];
    
    [_topPointLayer removeAllAnimations];
    [_leftPointLayer removeAllAnimations];
    [_bottomPointLayer removeAllAnimations];
    [_rightPointLayer removeAllAnimations];
    
    [_topPointLayer removeFromSuperlayer];
    _topPointLayer = nil;
    
    [_leftPointLayer removeFromSuperlayer];
    _leftPointLayer = nil;
    
    [_bottomPointLayer removeFromSuperlayer];
    _bottomPointLayer = nil;
    
    [_rightPointLayer removeFromSuperlayer];
    _rightPointLayer = nil;
}
//添加旋转动画
-(void)addRotationAnimation:(CALayer *)layer
{
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.duration = 1.5;
    rotationAnimation.repeatCount = MAXFLOAT;
    rotationAnimation.toValue = @(2*M_PI);
    //rotationAnimation.removedOnCompletion = NO;
    rotationAnimation.fillMode = kCAFillModeForwards;
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [layer addAnimation:rotationAnimation forKey:nil];
    
}
//给每个点添加位移动画
-(void)addAnimationForPointWith:(CALayer *)layer x:(CGFloat)x y:(CGFloat)y
{
    CAKeyframeAnimation *keyframeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    keyframeAnimation.duration = 1.5f;
    keyframeAnimation.repeatCount = MAXFLOAT;
    //keyframeAnimation.removedOnCompletion = NO;
    
    NSValue *fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(0, 0, 0)];
    NSValue *toValue = [NSValue valueWithCATransform3D:CATransform3DMakeTranslation(x, y, 0)];
    keyframeAnimation.values = @[fromValue,toValue,fromValue,toValue,fromValue];
    keyframeAnimation.fillMode = kCAFillModeForwards;
    keyframeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [layer addAnimation:keyframeAnimation forKey:nil];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
